#include "mainscene.h"
#include "config.h"
#include <QIcon>
#include <QPainter>
#include <QMouseEvent>
#include <ctime>
#include <QFont>
#include <QSound>
#include <qdialog.h>

MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
{
    //调用初始化场景
    initScene();

    //启动游戏
    playGame();
}

MainScene::~MainScene()
{


}

//初始化场景
void MainScene::initScene()
{
    //设置窗口固定尺寸
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);

    //设置标题
    setWindowTitle(GAME_TITLE);

    //加载图标
    setWindowIcon(QIcon(GAME_ICON));

    //定时器设置
    m_Timer.setInterval(GAME_RATE);

    //敌机出场间隔初始化
    m_recorder = -400;
    //敌机出场间隔初始化
    m_recorder2 = -400;
    //敌机出场间隔初始化
    m_recorder3 = -400;

    //随机数种子
    srand( (unsigned int)time(NULL) );

}

    void MainScene::playGame()
{

    //启动bgm
    QSound::play(SOUND_BACKGROUND);

    //启动定时器
    m_Timer.start();



    //监听定时器发送的信号
    connect(&m_Timer , &QTimer::timeout,[=](){
        //敌机1出现
        enemyToScene();
        //敌机2出现
        enemyToScene2();
        //敌机3出现
        enemyToScene3();

        //更新游戏中元素的坐标
        updatePosition();
        //绘制到屏幕中
        update();
        //碰撞检测
        collisionDetection();
    });
}


void MainScene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();

    //发射子弹
    m_hero.shoot();

    //计算发射子弹坐标
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
        if(m_hero.m_bullet[i].m_Free == false)
        {
           m_hero.m_bullet[i].updatePosition();
        }
    }

    //敌机出场
    for(int i = 0;i < ENEMY_NUM;i++)
    {
        if(m_enemy[i].m_Free == false)
        {
           m_enemy[i].updatePosition();
        }
    }

    //敌机2出场
    for(int i = 0;i < ENEMY_NUM2;i++)
    {
        if(m_enemy2[i].m_Free == false)
        {
           m_enemy2[i].updatePosition();
        }
    }

    //敌机3出场
    for(int i = 0;i < ENEMY_NUM3;i++)
    {
        if(m_enemy3[i].m_Free == false)
        {
           m_enemy3[i].updatePosition();
        }
    }

    //死亡条件
    if(gameover)
    {
       m_Timer.stop();
       rego=new over;
       this->hide();
       this->rego->show();
/*
       connect(this->rego,over::,[=]()
       {
          this->rego->hide();
          this->show();
       }
       */
    }
   if(score < 0)
    {
        gameover = true;
        update();
    }
}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);


    //绘制英雄飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);

    //绘制子弹
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
       if(m_hero.m_bullet[i].m_Free == false)
       {
          painter.drawPixmap(m_hero.m_bullet[i].m_X,m_hero.m_bullet[i].m_Y,m_hero.m_bullet[i].m_Bullet);
         QSound::play(SOUND_BULLET);
       }
    }

    //绘制敌机
    for(int i = 0 ;i < ENEMY_NUM;i++)
    {
       if(m_enemy[i].m_Free == false)
       {
          painter.drawPixmap(m_enemy[i].m_X,m_enemy[i].m_Y,m_enemy[i].m_enemy);
       }
    }

    //绘制敌机2
    for(int i = 0 ;i < ENEMY_NUM2;i++)
    {
       if(m_enemy2[i].m_Free == false)
       {
          painter.drawPixmap(m_enemy2[i].m_X,m_enemy2[i].m_Y,m_enemy2[i].m_enemy2);
       }
    }

    //绘制敌机3
    for(int i = 0 ;i < ENEMY_NUM3;i++)
    {
       if(m_enemy3[i].m_Free == false)
       {
          painter.drawPixmap(m_enemy3[i].m_X,m_enemy3[i].m_Y,m_enemy3[i].m_enemy3);
       }
    }

    //绘制计分板
    QFont font1("Courier",20);
    painter.setFont(font1);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(0,30,"得分：");
    painter.drawText(100,30,QString::number(score));


}

void MainScene::mouseMoveEvent(QMouseEvent * event)
{
    int x = event->x() - m_hero.m_Rect.width() *0.5;
    int y = event->y() - m_hero.m_Rect.height() *0.5;

    //边界检测
    if(x <=0 )
    {
        x=0;
    }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
    {
        x = GAME_WIDTH - m_hero.m_Rect.width();
    }
    m_hero.setPosition(x,y);
    if(y <=0 )
    {
        y=0;
    }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
    {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);

}

void MainScene::MouseTracking()
{

}

void MainScene::enemyToScene()
{
    m_recorder++;
    //出场前
    if(m_recorder < ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder = 0;

    for(int i = 0;i < ENEMY_NUM;i++)
    {
        if(m_enemy[i].m_Free)
        {
            m_enemy[i].m_Free = false;
            //敌机1坐标
            m_enemy[i].m_X = rand()%(GAME_WIDTH - m_enemy[i].m_Rect.width());
            m_enemy[i].m_Y = -m_enemy[i].m_Rect.height();
            break;

        }
    }
}

void MainScene::enemyToScene2()
{
    m_recorder2++;
    //出场前
    if(m_recorder2 < ENEMY_INTERVAL2)
    {
        return;
    }
    m_recorder2 = 0;

    for(int i = 0;i < ENEMY_NUM2;i++)
    {
        if(m_enemy2[i].m_Free)
        {
            m_enemy2[i].m_Free = false;
            //敌机2坐标
            m_enemy2[i].m_X = rand()%(GAME_WIDTH - m_enemy2[i].m_Rect.width());
            m_enemy2[i].m_Y = -m_enemy2[i].m_Rect.height();
            break;

        }
    }
}

void MainScene::enemyToScene3()
{
    m_recorder3++;
    //出场前
    if(m_recorder3 < ENEMY_INTERVAL3)
    {
        return;
    }
    m_recorder3 = 0;

    for(int i = 0;i < ENEMY_NUM3;i++)
    {
        if(m_enemy3[i].m_Free)
        {
            m_enemy3[i].m_Free = false;
            //敌机3坐标
            m_enemy3[i].m_X = rand()%(GAME_WIDTH - m_enemy3[i].m_Rect.width());
            m_enemy3[i].m_Y = -m_enemy3[i].m_Rect.height();
            break;

        }
    }
}

void MainScene::collisionDetection()
{
    for(int i = 0;i < ENEMY_NUM;i++)
    {
        //如果空闲继续执行循环
        if(m_enemy[i].m_Free)
        {
            continue;
        }
        for(int j = 0;j < BULLET_NUM;j++)
        {
            if(m_hero.m_bullet[j].m_Free)
            {
              continue;
            }
        //如果子弹和飞机相交发生碰撞，同时将飞机和子弹空闲状态设置为真
            if(m_enemy[i].m_Rect.intersects(m_hero.m_bullet[j].m_Rect))
            {
              m_enemy[i].m_Free = true;
              m_hero.m_bullet[j].m_Free = true;
              score += 1;
              QSound::play(SOUND_BOMB);

            }

        }
        if(m_enemy[i].m_Rect.intersects(m_hero.m_Rect))
      {
        m_enemy[i].m_Free = true;
        m_hero.m_Free = true;
        score -= 20 ;
      }

    }
    for(int i = 0;i < ENEMY_NUM2;i++)
    {
        //如果2空闲继续执行循环
        if(m_enemy2[i].m_Free)
        {
            continue;
        }
        for(int j = 0;j < BULLET_NUM;j++)
        {
            if(m_hero.m_bullet[j].m_Free)
            {
              continue;
            }
        //如果子弹和飞机相交发生碰撞，同时将飞机和子弹空闲状态设置为真
            if(m_enemy2[i].m_Rect.intersects(m_hero.m_bullet[j].m_Rect))
            {
              m_enemy2[i].m_Free = true;
              m_hero.m_bullet[j].m_Free = true;
              score += 5;
              QSound::play(SOUND_BOMB);

            }

        }
        if(m_enemy2[i].m_Rect.intersects(m_hero.m_Rect))
      {
        m_enemy2[i].m_Free = true;
        m_hero.m_Free = true;
        score -= 50 ;
      }

    }
    for(int i = 0;i < ENEMY_NUM3;i++)
    {
        //如果3空闲继续执行循环
        if(m_enemy3[i].m_Free)
        {
            continue;
        }
        for(int j = 0;j < BULLET_NUM;j++)
        {
            if(m_hero.m_bullet[j].m_Free)
            {
              continue;
            }
        //如果子弹和飞机相交发生碰撞，同时将飞机和子弹空闲状态设置为真
            if(m_enemy3[i].m_Rect.intersects(m_hero.m_bullet[j].m_Rect))
            {
              m_enemy3[i].m_Free = true;
              m_hero.m_bullet[j].m_Free = true;
              score += 100;
              QSound::play(SOUND_BOMB);
            }

        }
        if(m_enemy3[i].m_Rect.intersects(m_hero.m_Rect))
      {
        m_enemy3[i].m_Free = true;
        m_hero.m_Free = true;
        score -= 200 ;

      }
    }
}
